
var app = getApp();
const db = wx.cloud.database({
  throwOnNotFound: false
})
const _ = db.command
Page({
  data: {
    userName: '',
    userID: '',
    userPassword: '',
    userPasswordAgain: '',
    checkbox: false,
    repetition: false,
    numList: [{
      name: '创建用户'
    },{
      name: '完善个人信息'
    }],
    num: 0,
    scroll: 0,
  },
  // 返回主页面
  backHomeTap: function() {
    wx.switchTab({
    url: '../home/home',
    })
  },
  // 绑定
  bindingTap: function () {
    wx.redirectTo({
    url: '../mine/login',
    })
  },
  // 用户名
  userNameInput: function(e) {
    this.setData({
    userName: e.detail.value
    });
  },
  //账号
  userIDInput: function(e) {
    this.setData({
    userID: e.detail.value
    });
  
  },
  // 密码
  userPasswordInput: function(e) {
    this.setData({
    userPassword: e.detail.value
    });
  },
  // 再次输入密码
  userPasswordAgainInput: function(e) {
    this.setData({
    userPasswordAgain: e.detail.value
    });
  },
  // 同意
  checkboxChange: function() {
    if (this.data.checkbox === false) {
    this.setData({
      checkbox: true
    })
    } else {
    this.setData({
      checkbox: false
    })
    }
    // var that = this;
    // var userID = this.data.userID;

    wx.cloud.callFunction({
      name:"searchUserID",
      data:{
        userID:this.data.userID
      },
      complete:res=>{
        console.log("check",this.data.userID)
        if(res.result.data.length == 1){
          this.setData({
            repetition:true
          })
        }else{
          this.setData({
            repetition:false
          })
        }
      }
    });
   
    // db.collection('userInfo').where({
    //   userID: _.eq(userID)
    // }).get({
    //   success: function (res) {
    //     console.log("userID:",userID)
    //     console.log("查找结果",res)
    //     if (res.data.length === 1) {
    //       that.setData({
    //         repetition: true
    //       })
    //     }else{
    //       that.setData({
    //         repetition: false
    //       }) 
    //     }
    //   }
    // })
  },
  // 下一步，完善个人信息
  perfectInforTap: function() {
    var userName = this.data.userName;
    var userID = this.data.userID;
    var userPassword = this.data.userPassword;
    var checkbox = this.data.checkbox;
    var userPasswordAgain = this.data.userPasswordAgain;
    var name = /^[A-Za-z0-9\u4e00-\u9fa5]+$/;
    var ID = /^[A-Za-z0-9]+$/;
    var repetition = this.data.repetition;
    
    wx.cloud.callFunction({
      name:"searchUserID",
      data:{
        userID:this.data.userID
      },
      complete:res=>{
        console.log("查找userID信息成功",res)
        console.log("userid",this.data.userID)
        if(res.result.data.length == 1){
          this.setData({
            repetition:true
          })
        }else{
          this.setData({
            repetition:false
          })
        }
      }
    });
    if (userName === '') {
      wx.showToast({
        title: '请输入用户名',
        icon: 'none',
        duration: 2000,
        mask: true
      })
    } else if (!name.test(userName)) {
      wx.showToast({
        title: '用户名格式不正确',
        icon: 'none',
        duration: 2000,
        mask: true
      })
    } else if (this.data.userID === '') {
      wx.showToast({
        title: '请输入账号',
        icon: 'none',
        duration: 2000,
        mask: true
      })  
    } else if (!ID.test(this.data.userID)) {
      wx.showToast({
        title: '账号格式不正确',
        icon: 'none',
        duration: 2000,
        mask: true
      })
    } else if (this.data.repetition == true) {
      wx.showToast({
        title: '账号已存在',
        icon: 'none',
        duration: 2000,
        mask: true
      })
     
    } else if (userPassword === '') {
    wx.showToast({
      title: '请输入密码',
      icon: 'none',
      duration: 2000,
      mask: true
    })
    } else if (userPassword.length < 6) {
    wx.showToast({
      title: '密码最少6位',
      icon: 'none',
      duration: 2000,
      mask: true
    })
    } else if (userPassword !== userPasswordAgain) {
    wx.showToast({
      title: '两次密码输入不一致',
      icon: 'none',
      duration: 2000,
      mask: true
    })
    } else if (checkbox === false) {
    wx.showToast({
      title: '请选中已阅读',
      icon: 'none',
      duration: 2000,
      mask: true
    })
    } else {
      wx.redirectTo({
        url: '/pages/register/userregister',
      })
      app.userName = userName;
      app.userID = userID;
      app.userPassword = userPassword;
    }
  }
})